export to_csv
Export i18n files into a csv file
Command
# Display help for export to_csv
npx @jy95/i18n-tools export to_csv --help
Purpose
Suppose you have several i18n locales such as :
- fr.json
- nl.json
- de.json
fr.json
{
"commons":{
"myNestedKey":"Hello world FR",
"myNestedArray":[
"1 FR",
"2 FR",
"3 FR"
]
},
"array":[
"1 FR",
"2 FR",
"3 FR"
],
"simpleKey":"[FR] not setted key",
"Key with spaces":[
{
"test":"42 is the answer"
}
],
"Missing key in DE":"present"
}
nl.json
{
"commons":{
"myNestedKey":"Hello world NL",
"myNestedArray":[
"1 NL",
"2 NL",
"3 NL"
]
},
"array":[
"1 NL",
"2 NL",
"3 NL"
],
"simpleKey":"[NL] not setted key",
"Key with spaces":[
{
"test":"42 is the answer"
}
],
"Missing key in DE":"present"
}
de.json
{
"commons":{
"myNestedKey":"Hello world DE",
"myNestedArray":[
"1 DE",
"2 DE",
"3 DE"
]
},
"array":[
"1 DE",
"2 DE",
"3 DE"
],
"simpleKey":"[DE] not setted key",
"Key with spaces":[
{
"test":"42 is the answer"
}
]
}
This command helps you to turn them into a single csv file such as this one.
export-csv.csv
Technical Key;French translation;Dutch translation;German translation
Key with spaces[0].test;42 is the answer;42 is the answer;42 is the answer
Missing key in DE;present;present;
array[0];1 FR;1 NL;1 DE
array[1];2 FR;2 NL;2 DE
array[2];3 FR;3 NL;3 DE
commons.myNestedArray[0];1 FR;1 NL;1 DE
commons.myNestedArray[1];2 FR;2 NL;2 DE
commons.myNestedArray[2];3 FR;3 NL;3 DE
commons.myNestedKey;Hello world FR;Hello world NL;Hello world DE
simpleKey;[FR] not setted key;[NL] not setted key;[DE] not setted key
Examples of settings
- Paths
- Objects/Arrays
- Settings.js
npx @jy95/i18n-tools export to_csv --settings "/absolutePath/to/settings1.json"
settings1.json
{
"files":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\files.json",
"columns":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\columns.json",
"filename":"settings1-output",
"outputDir":"D:\\TEMP\\TEMP"
}
files.json
{
"FR":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\fr.json",
"NL":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\nl.json",
"DE":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\de.json"
}
columns.json
[
{
"locale":"FR",
"label":"French translation"
},
{
"locale":"NL",
"label":"Dutch translation"
},
{
"locale":"DE",
"label":"German translation"
}
]
npx @jy95/i18n-tools export to_csv --settings "/absolutePath/to/settings2.json"
settings2.json
{
"files":{
"FR":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\fr.json",
"NL":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\nl.json",
"DE":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\de.json"
},
"columns":[
{
"locale":"FR",
"label":"French translation"
},
{
"locale":"NL",
"label":"Dutch translation"
},
{
"locale":"DE",
"label":"German translation"
}
],
"filename":"settings2-output",
"outputDir":"D:\\TEMP\\TEMP"
}
npx @jy95/i18n-tools export to_csv --settings "/absolutePath/to/settings3.js"
settings3.js
module.exports = {
files: ["fr", "nl", "de"].reduce(
(prev, curr) =>
Object.assign(prev, {
[curr.toUpperCase()]: `D:\\TEMP\\TEMP\\tests-for-export\\correct\\${curr}.json`,
}),
{}
),
columns: [
["FR", "French translation"],
["NL", "Dutch translation"],
["DE", "German translation"],
].map(([locale, label]) => ({ locale: locale, label: label })),
filename: "settings3-output",
outputDir: "D:\\TEMP\\TEMP"
};
FAQ
I only want a subset of the data. How can I achieve that ?
Simply add the resultsFilter
option in your settings.json
or settings.js
:
tip
Reminder - the type of the function parameter :
type I18N_Merged_Data = {
technical_key: string;
labels: {
[locale: string]: string;
};
}[];
settings.js
"resultsFilter": function(data /*: I18N_Merged_Data*/) {
return data.filter((row) =>
// Takes rows that have at least a missing label in one i18n file such as "Missing key in DE" case
// Object.keys(row.labels).length !== 3 ||
Object
.values(row.labels)
// Takes rows that have at least one empty label or contains a given prefix
.some(
(label) =>
label.length === 0 ||
["[FR]", "[NL]", "[DE]"].some((prefix) => label.startsWith(prefix))
)
);
}
OR
settings.json
"resultsFilter": "D:\\TEMP\\TEMP\\resultsFilter.js"
resultsFilter.js
module.exports = function(data /*: I18N_Merged_Data*/) {
return data.filter((row) =>
// Takes rows that have at least a missing label in one i18n file such as "Missing key in DE" case
// Object.keys(row.labels).length !== 3 ||
Object
.values(row.labels)
// Takes rows that have at least one empty label or contains a given prefix
.some(
(label) =>
label.length === 0 ||
["[FR]", "[NL]", "[DE]"].some((prefix) => label.startsWith(prefix))
)
);
}
I want the locales in a given order in the result file. How can I achieve that ?
Simply update the columns
option with your given order in your settings.json
or settings.js
, such as :
settings.js
"columns": [
{
"locale":"NL",
"label":"Dutch translation"
},
{
"locale":"FR",
"label":"French translation"
}
]
I only work with flat JSON file(s). How can I make this command work ?
Simply set option keySeparator
to false
in your settings.json
or settings.js
, such as :
settings.json
{
"keySeparator": false
}